package com.czp.user.action;

import java.util.List;
import java.util.Map;

import com.czp.framework.action.BaseActionSupport;
import com.czp.framework.exception.IServiceException;
import com.czp.framework.util.HttpUtils;
import com.czp.user.model.RelationRole;
import com.czp.user.model.User;
import com.czp.user.service.IUserService;

/**
 * 【描 述】：用户登录Action
 * 【环 境】：J2SE 1.7
 * @author   柴志鹏
 * @version  version 1.0
 * @since    2014-4-29 新建
 */
public class UserAction extends BaseActionSupport {
	private static final long serialVersionUID = 2153270600374259634L;
	//	private static Logger logger = Logger.getLogger(UserAction.class);
	private IUserService userService;
	private List<User> users;
	private User user;
	
	/** 
	 * 【描 述】：创建、编辑、注销 用户信息
	 */
	public void userController() {
		try {
			
			int count = userService.userController(user);
			this.responseOutPrint("{success:"+ count +"}");
		} catch(Exception e) {
			throw new IServiceException(this.getClass() + "userController", e);
		}
	}
	
	/** 
	 * 【描 述】：根据人员Id查询用户信息
	 */
	public void getUserInfoByPid() {
		try {
			String personId = request.getParameter("personId");
			this.responseOutPrint(
					userService.getUserInfoByPid(personId));
		} catch(Exception e) {
			throw new IServiceException(this.getClass() + "getUserInfoByPid", e);
		}
	}
	
	/** 
	 * 【描 述】：通过人员Id查询关联角色信息
	 */
	public void queryRelationRole() {
		try {
			String personId = request.getParameter("personId");
			this.responseOutPrint(
					userService.queryRelationRole(personId));
		} catch(Exception e) {
			throw new IServiceException(this.getClass() + "queryRelationRole", e);
		}
	}
	
	/** 
	 * 【描 述】：根据人员Id查询全部被选中角色
	 */
	public void getSelectedRoleList() {
		RelationRole relRole = new RelationRole();
		try {
			relRole.setPersonId(request.getParameter("personId"));
			relRole.setRoleName(request.getParameter("roleName"));
			relRole.setStart(Integer.parseInt(request.getParameter("start")));
			relRole.setLimit(Integer.parseInt(request.getParameter("limit")));
			this.responseOutPrint(
					userService.getSelectedRoleList(relRole));
		} catch(Exception e) {
			throw new IServiceException(this.getClass()+ "getSelectedRoleList", e);
		}
	}
	
	/** 
	 * 【描 述】：授权、取消角色菜单
	 */
	public void relationRoleController() {
		try {
			String jsonData = request.getParameter("jsonData");
			this.responseOutPrint(
					userService.relationRoleController(jsonData));
		} catch(Exception e) {
			throw new IServiceException(this.getClass()+ "relationRoleController", e);
		}
	}
	
	/** 
	 * 【描 述】：批量取消角色授权
	 */
	public void deleteRelationRole() {
		try {
			String jsonData = request.getParameter("jsonData");
			this.responseOutPrint(
					userService.deleteRelationRole(jsonData));
		} catch(Exception e) {
			throw new IServiceException(this.getClass()+ "deleteRelationRole", e);
		}
	}
	
	/** 
	 * 【描 述】：根据角色Id查询关联"菜单"
	 */
	public void queryRelationMenu() {
		try {
			String personId = request.getParameter("personId");
			this.responseOutPrint(
					userService.queryRelationMenu(personId));
		} catch(Exception e) {
			throw new IServiceException(this.getClass()+ "queryRelationMenu", e);
		}
	}
	
	/** 
	 * 【描 述】：用户登录
	 */
	public void login() {
		try {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String ip = HttpUtils.getIpAddr(request);
			
			if(username != null && !username.equals("")) {
				//根据用户名查询用户信息
				User loginUser = userService.login(username);
				//判断用户是否存在
				if(loginUser != null) {
					//判断用户状态
					if(loginUser.getEnableStatus() != '0') {//是否禁用 - 1启用/0禁用
						//匹配密码是否正确
						if(password.equals(loginUser.getPassword())) {
							//将用户信息添加到Session中
							//request.getSession().setAttribute("userSession", loginUser);
							HttpUtils.setUserSession(loginUser);
							this.responseOutPrint("1");//登录成功
							userService.addLoginLogInfo(username, password, ip, "0");
						} else {
							this.responseOutPrint("0");//用户名或密码错误
							userService.addLoginLogInfo(username, password, ip, "1");
						}
					} else {
						this.responseOutPrint("2");//该用户已被禁用
						userService.addLoginLogInfo(username, password, ip, "2");
					}
				} else {
					this.responseOutPrint("0");//用户名或密码错误
					userService.addLoginLogInfo(username, password, ip, "3");
				}
			}
		} catch (Exception e) {
			throw new IServiceException(this.getClass()+ "login", e);
		}
	}
	
	/** 
	 * 【描 述】：验证用户名是否存在
	 */
	public void isUserExist() {
		try{
			//根据返回数量判断用户是否存在
			if(userService.isUserExist(request.getParameter("username")) > 0) {
				this.responseOutPrint(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/** 
	 * 【描 述】：密码修改
	 */
	public void modifyPassword() {
		try {
			User user = new User();
			user.setId(Integer.parseInt(request.getParameter("userId")));
			user.setOldPass(request.getParameter("oldPass"));
			user.setPassword(request.getParameter("password"));
			this.responseOutPrint(userService.modifyPassword(user));
			
		} catch(Exception e) {
			throw new IServiceException(this.getClass()+ "modifyPassword", e);
		}
	}
	
	/** 
	 * 【描 述】：注销当前用户
	 * @return
	 */
	public String logout() {
		try {
			//request.getSession().removeAttribute("userSession");
			//HttpUtils.removeAllSession();//清空session全部信息
			//释放session
			request.getSession().invalidate();
			return SUCCESS;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取 user
	 * @return 返回 user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * 设置 user
	 * @param 对user进行赋值
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * 获取 userService
	 * @return 返回 userService
	 */
	public IUserService getUserService() {
		return userService;
	}
	
	/**
	 * 获取 users
	 * @return 返回 users
	 */
	public List<User> getUsers() {
		return users;
	}

	/**
	 * 设置 users
	 * @param 对users进行赋值
	 */
	public void setUsers(List<User> users) {
		this.users = users;
	}

	/**
	 * 设置 userService
	 * @param 对userService进行赋值
	 */
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
}
